package com.example.shushiyunsst.fragment.main.home.child.icon_sst

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.example.library_common.album.Album
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.glidmodule.GlideApp
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.ICON_TRANSFER_NOTIFY
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.upload_file.UploadFileWorker
import com.example.shushiyunsst.R
import com.example.shushiyunsst.util.COMPLAINT
import com.example.shushiyunsst.util.TO_ID
import com.example.wallet.db.UserInfoManager
import org.greenrobot.eventbus.EventBus

@Route(path = COMPLAINT)
class ComplaintFragment : BaseFragmentEx(), View.OnClickListener {
    override fun getLayoutId(): Any {
        return R.layout.complaint_fragment
    }

    @JvmField
    @Autowired(name = TO_ID)
    var toid: String = ""

    private lateinit var uploadImg: ImageView

    private lateinit var showImg: ImageView

    private lateinit var uploadTv: TextView

    private lateinit var upLoad: Button

    private lateinit var icAddImg: View

    private lateinit var mProgressBar:View

    private var imgPath = ""

    private var mUploadFileWorker: UploadFileWorker? = null


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "上传凭证"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mProgressBar=view.findViewById(R.id.mProgressBar)
        showImg = view.findViewById(R.id.show_img)
        uploadImg = view.findViewById(R.id.uploadImg)
        uploadTv = view.findViewById(R.id.uploadTv)
        upLoad = view.findViewById(R.id.upLoad)
        icAddImg = view.findViewById(R.id.ic_add_img)

        upLoad.setOnClickListener(this)
        icAddImg.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.upLoad -> {
                if (imgPath.isEmpty()) {
                    Toast.makeText(_mActivity, "请选择照片", Toast.LENGTH_LONG).show()
                    return
                }
                if (mUploadFileWorker != null) {
                    Toast.makeText(_mActivity, "正在发送", Toast.LENGTH_LONG).show()
                    return
                }

                mProgressBar.visibility=View.VISIBLE

                val mutableListOf = mutableListOf<String>()

                mutableListOf.add(imgPath)
                mUploadFileWorker = UploadFileWorker()
                mUploadFileWorker?.setListener(object :
                    UploadFileWorker.UpLoadOverListener {
                    override fun onUpLoadOver(res: List<String>) {
                        mUploadFileWorker = null
                        onSending(res)
                    }
                })?.doWork(mutableListOf)
            }
            R.id.ic_add_img -> {
                Album.getInstance()
                    .singleImage(true)
                    .onSuccess {
                        if (it.isNotEmpty()) {
                            imgPath = it[0].path
                            showImg.visibility = View.VISIBLE
                            GlideApp.with(showImg)
                                .load(imgPath)
                                .centerCrop()
                                .into(showImg)
                        }
                    }
                    .onFailed { _, msg ->
                        Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show()
                    }
                    .execute(childFragmentManager)
            }
        }
    }

    private fun onSending(res: List<String>) {
        if (res.isEmpty()){
            mProgressBar.visibility=View.GONE
            Toast.makeText(_mActivity, "发送失败", Toast.LENGTH_LONG).show()
            return
        }

        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "payUrl" to res[0],
            "toid" to toid,
            "uid" to userInfo?.uid
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.orderAppeal)
            .success {

                Toast.makeText(_mActivity, "发送成功", Toast.LENGTH_LONG).show()
                EventBus.getDefault().post(MessageWrap.getInstance(ICON_TRANSFER_NOTIFY))
                onBackPressedSupport()

            }.failure {
                mProgressBar.visibility=View.GONE
                Toast.makeText(_mActivity, "发送失败", Toast.LENGTH_LONG).show()
            }.error { _, msg ->
                mProgressBar.visibility=View.GONE
                Toast.makeText(_mActivity, msg, Toast.LENGTH_LONG).show()
            }.build().post()
    }
}